package com.china08.yxyapi.repository.mg;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.Query;

import com.china08.yxyapi.entity.mg.office.YxyNoticeInner;

@Transactional
public interface YxyNoticeInnerRepository extends MongoRepository<YxyNoticeInner, String> {

    @Query("{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isDelete':?2}}}")
    List<YxyNoticeInner> findBySchoolIdAndReceiverUserIdAndReceiverIsDel(String schoolId, String userId, int isDelete, Pageable pageable);

    // 当前学校，当前用户，总数量
    @Query("{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isDelete':?2}}}")
    List<YxyNoticeInner> findBySchoolIdAndReceiverUserIdAndReceiverIsDel(String schoolId, String userId, int isDelete);

    @Query(value = "{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isDelete':?2}}}", count = true)
    int countBySchoolIdAndReceiverUserIdAndReceiverIsDel(String schoolId, String userId, int isDelete);

    Page<YxyNoticeInner> findBySchoolIdAndUserIdAndDeleteStatus(String schoolId, String userId, int deleteStatus, Pageable pageable);

    List<YxyNoticeInner> findByIdInAndUserIdAndSchoolId(List<String> idLst, String currentUserId, String schoolId);

    @Query("{'id':{$in:?0},'receiver':{'$elemMatch': {'userId':?1}},'school_id':?2}")
    List<YxyNoticeInner> findByIdInAndReceiverUserIdAndSchoolId(List<String> idLst, String currentUserId, String schoolId);

    // 有误差的方法
    // Integer countByReceiverUserIdAndReceiverIsReadAndSchoolId(String
    // currentUserId, int isRead, String schoolId);

    // 无错误的方法
    @Query(value = "{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isDelete':?2,'isRead':?3}}}", count = true)
    Integer SchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiverIsRead(String schoolId, String currentUserId, int isDelete, int isRead);

    @Query("{'id':?0,'receiver':{'$elemMatch': {'userId':?1,'isRead':?2}},'school_id':?3}")
    List<YxyNoticeInner> findByIdAndReceiverUserIdAndReceiverIsReadAndSchoolId(String id, String currentUserId, int isRead, String schoolId);

    YxyNoticeInner findById(String id);

    @Query("{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isDelete':?2,'isRead':?3}}}")
    List<YxyNoticeInner> findBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiver(String schoolId, String userId, int isDelete, int receiverIsRead, Pageable pageable);

    @Query(value = "{'school_id':?0,'receiver':{'$elemMatch': {'userId':?1,'isDelete':?2,'isRead':?3}}}", count = true)
    Integer countBySchoolIdAndReceiverUserIdAndReceiverIsDeleteAndReceiverIsRead(String schoolId, String userId, int isDelete, int receiverIsRead);

}
